﻿using System.Collections;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.SessionState;
using Moq;
using NUnit.Framework;
using TeamsPortal.DataAccess;
using TeamsPortal.ServiceClient;
using TeamsPortal.Tests.Fakes;
using TeamsPortal.Web.Controllers;
using TeamsPortal.Web.State;

namespace TeamsPortal.Tests.Controllers
{
	[TestFixture]
	public class UsersControllerTest
	{
		[Test]
		public void User_Get_Test()
		{
			SessionStateItemCollection session = ForumsControllerTest.GetSessionWithTestUser();
			UserState userState = (UserState)session["User"];

            var mockTopicsServiceClient = new Mock<ITopicsServiceClient>();
            mockTopicsServiceClient
                .Setup(m => m.GetByUser(userState.Id, userState.Role))
                .Returns(new List<Topic>());
            var mockUserServiceClient = new Mock<IUsersServiceClient>();
            mockUserServiceClient
                .Setup(m => m.Get(userState.Id))
                .Returns(userState.ToUser());
            var controller = new UsersController(mockTopicsServiceClient.Object) { 
                UsersServiceClient = mockUserServiceClient.Object,
            };
            controller.ControllerContext = FakeControllerContext.Create(controller, "http://localhost", session);
            
            ActionResult result = controller.Detail(userState.Id);

            User user = (User)controller.ViewData.Model;
			Assert.IsNotNull(user);

            mockTopicsServiceClient.VerifyAll();
            mockUserServiceClient.VerifyAll();
		}

		[Test]
		public void User_Get_MessagesByUser()
		{
			SessionStateItemCollection session = ForumsControllerTest.GetSessionWithTestUser();
			UserState userState = (UserState)session["User"];

            var mockTopicsServiceClient = new Mock<ITopicsServiceClient>();
            mockTopicsServiceClient
                .Setup(m => m.GetTopicsAndMessagesByUser(userState.Id))
                .Returns(new List<Topic>());
            var controller = new UsersController(mockTopicsServiceClient.Object);
            var mockUserServiceClient = new Mock<IUsersServiceClient>();
            mockUserServiceClient.Setup(m => m.Get(userState.Id)).Returns(userState.ToUser());
            controller.UsersServiceClient = mockUserServiceClient.Object;
			
            ActionResult result = controller.MessagesByUser(userState.Id);
            
            Assert.IsInstanceOf<ViewResult>(result);

            mockTopicsServiceClient.VerifyAll();
            mockUserServiceClient.VerifyAll();

			IList topics = (IList)controller.ViewData.Model;
			Assert.IsNotNull(topics);
		}
	}
}
